package com.lix.controller.admin;

import com.lix.base.BaseResult;
import com.lix.config.SystemInnerConfig;
import com.lix.service.admin.AdminCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.ElementType;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "管理员通用控制器")
public class AdminCommonController {

    private static final Logger logger = LoggerFactory.getLogger(AdminCommonController.class);

    @Autowired
    private AdminCommonService adminCommonService;

    @Autowired
    private SystemInnerConfig systemInnerConfig;

    @GetMapping("/getEncryptStr")
    @ApiOperation("获取加密字符串处理器")
    public BaseResult getEncryptStr(@RequestParam("keyword") String keyword) {
        BaseResult result = BaseResult.ok();
        adminCommonService.getEncryptStr(keyword, result);
        return result;
    }

    @GetMapping("/getDecryptStr")
    @ApiOperation("获取解密字符串处理器")
    public BaseResult getDecryptStr(@RequestParam("encryptStr") String encryptStr) {
        BaseResult result = BaseResult.ok();
        adminCommonService.getDecryptStr(encryptStr, result);
        return result;
    }

    @PostMapping("/refresh")
    @ApiOperation("刷新初始化系统内部的配置数据")
    public BaseResult refreshConfig() {
        BaseResult result = BaseResult.ok();
        try {
            systemInnerConfig.reloadConfig();
            result.setMsg("刷新成功");
            result.setCode(200);
            result.setData("success");
        } catch (Exception e) {
            logger.error("刷新配置失败----{}", e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
